<template>
	<view class="page">
		<view class="content user-info-content">
			<view class="user-info-img">
				<image :src="imgurl" mode="widthFix"></image>
			</view>
			<view class="user-info">
				<view class="name">
					{{this.userinfo.nickname}}
					<view class="job">外业调查员</view>
				</view>
				<view class="phone">
					{{userinfo.phone||''}}
				</view>
				<!-- <view class="address">
					青海省/西宁市/平安县
				</view> -->
			</view>
			<view class="record">
				调查记录
			</view>
			<view class="plot-wrap">
				<view class="plot-item">
					<view class="count">
						<text>{{info.project}}</text>条
					</view>
					<view>项目数量</view>
				</view>
				<view class="plot-item">
					<view class="count">
						<text>{{info.ydnum}}</text>个
					</view>
					<view>样地数量</view>
				</view>
				<view class="plot-item">
					<view class="count">
						<text>{{info.surveynum}}</text>个
					</view>
					<view>调查数量</view>
				</view>
			</view>
		</view>
		<view class="content">
			<view class="menu-item">
				数据上报
			</view>
			<view class="data-wrap">
				<view class="data-item">
					<view class="count">
						<text>{{info.surveyYd}}</text>条
					</view>
					<view>样地数量</view>
				</view>
				<view class="data-item">
					<view class="count">
						<text>{{info.surveyYf}}</text>个
					</view>
					<view>样方数量</view>
				</view>
				<view class="data-item">
					<view class="count">
						<text>{{info.uploadNum}}</text>个
					</view>
					<view>调查数量</view>
				</view>
			</view>
		</view>
		<view class="content menu-wrap">
			<!-- 	<view class="menu-item">
				离线地图
			</view> -->
			<view class="menu-item" @click="asyncData">
				数据同步
			</view>
			<view class="menu-item" @click="gotoAbout">
				关于系统
			</view>
			<view class="menu-item" @click="loginOut">
				退出登录
			</view>
		</view>
		<uni-popup ref="popup" class="popup" :mask-click="false" background-color="#fff">
			<view class="popup-title">{{popueTitle}}</view>
			<view class="progress-box">
				<progress :percent="progress" show-info stroke-width="3" />
			</view>

		</uni-popup>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="warn" cancelText="关闭" confirmText="同意" title="通知" content="退出登录将导致已调查数据被清理"
				@confirm="dialogConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getInfo,
		setInfo
	} from '@/core/unicommon/storyageInfo.js'
	import {
		loadSave,
		downLoad,
		filesList,
		uploadImage,
		removeSaveFile
	} from '@/core/file/File.js'
	import {
		getSubsurfaceBiomass,
		getGrasslandType,
		getEnvironment,
		getYfList,
		getBiomass,
		getYdList,
		getFrequencys,
		getRichness,
		getBrushSurveyList,

		projects,
		organizationTree,
		addOrUpdateYd,
		addOrUpdateYf,
		addFrequencys,
		addOrUpdateGrasslandType,
		addOrUpdateSubsurfaceBiomass,
		addOrUpdateEnvironment,
		addOrUpdateBiomass,
		delYf,
		addOrUpdateBrushSurvey,
		addOrUpdateFrequency,
		addOrUpdateRichness
	} from '@/api/system.js'
	export default {
		data() {
			return {
				imgurl: "../../static/c1.png",
				userdata: {},
				userinfo: {},
				popueTitle: "正在下载资源",
				info: {
					project: 0,
					ydnum: 0,
					surveynum: 0,
					surveyYd: 0,
					surveyYf: 0,
					uploadNum: 0
				},
				progress: 10,
			}
		},
		onLoad(e) {
			this.initData()

		},
		onReady() {
			console.log("abc")

		},
		mounted() {
			console.log("aaa")

		},
		methods: {

			onTabItemTap() {
				this.initData()
			},
			initData() {
				getInfo("userinfo").then(res => {
					this.userinfo = res.data

				})
				getInfo("userdata").then(res => {
					this.userdata = res.data
					this.info.project = res.data.projects.length
					this.info.ydnum = res.data.yd.length
					this.info.surveynum = res.data.yd.length + res.data.yf.length
					let actionYd = res.data.yd.filter(t => t.action != 'none')
					this.info.surveyYd = actionYd.length
					let actionyf = res.data.yf.filter(t => t.action != 'none')
					this.info.surveyYf = actionyf.length
					this.info.uploadNum = actionYd.length + actionyf.length
					console.log("a", this.userdata)
				})
			},
			gotoAbout() {
				uni.navigateTo({
					url: "/pages/system/about"
				})

			},
			async loginOut() {
				await setInfo('userinfo', {})

				uni.navigateTo({
					url: "/pages/login/login"
				});

			},
			dialogClose() {
				this.$refs.alertDialog.close()
			},
			dialogConfirm() {
				this.clearData();
				this.loginOut()
			},
			async clearData() {
				uni.showLoading({
					title: "退出中",
					mask: true,
					icon: 'loading',
				})
				try {
					uni.removeStorageSync('userdata');
				} catch (e) {

				}

				let fileRes = await filesList()

				for (const file of fileRes) {
					let a = await removeSaveFile(file.filePath)

				}
				uni.hideLoading()
			},
			async asyncData() {
				// this.popueTitle = "正在上传采集数据"
				// this.progress = 0
				// this.toggleOpen()
				//上传数据
				await this.upload()

				await this.downLoadData(this.userinfo.id, true)

			},
			async downLoadData(userid, type) {
				this.popueTitle = "开始下载工程数据"
				this.toggleOpen()
				this.progress = 0

				let surveyUser = userid
				let projectsRes = await projects({})
				//缓存样地数据
				this.popueTitle = "开始下载样地数据"
				let ydData = await this.downloadyd(surveyUser, type)
				this.progress = 16
				//缓存样方数据
				this.popueTitle = "开始下载样方数据"
				let yfData = await this.downloadyf(surveyUser, type)
				this.progress = 32

				this.popueTitle = "开始下载生物量数据"
				let biomassRes = await this.downloadbiomass(surveyUser, type)
				this.progress = 48

				this.popueTitle = "开始下载温湿度数据"
				let envRes = await this.downloadenv(surveyUser, type)
				this.progress = 64

				this.popueTitle = "开始下载草地类型数据"

				let grassTypeRes = await this.downloadGrassType(surveyUser, type)
				this.progress = 80
				this.popueTitle = "开始下载草地类型数据"
				let subBiomassRes = await this.downloadSubBiomass(surveyUser, type)

				let brushRes = await this.downloadBrush(surveyUser, type)

				let richRes = await this.downloadRich(surveyUser, type)
				let frequencyRes = await this.downloadFrequencys(surveyUser, type)

				let orgTree = await organizationTree()
				this.progress = 100
				//this.toggleClose()

				//下载资源
				this.popueTitle = "开始下载资源数据"
				this.progress = 0
				// this.toggleOpen()
				let imageInfos = []
				imageInfos = imageInfos.concat(ydData.images, yfData.images, biomassRes.images, envRes.images,
					grassTypeRes.images, subBiomassRes.images)

				let fileRes = await this.downLoadfiles(imageInfos)


				//storyage存储
				let userdata = {
					"projects": projectsRes.data.records,
					yd: ydData.data,
					yf: yfData.data,
					grassType: grassTypeRes.data,
					env: envRes.data,
					biomass: biomassRes.data,
					subBiomass: subBiomassRes.data,
					brush: brushRes.data,
					rich: richRes.data,
					frequency: frequencyRes.data,
					org: orgTree.data,
					files: fileRes
				}
				this.userdata = userdata
				uni.setStorageSync("userdata", userdata)
				this.toggleClose()
			},

			async downloadyd(surveyUser, type) {
				let imageInfos = []
				let ydRes = await getYdList({
					surveyUser
				})
				let yd = null
				let ydData = ydRes.data
				if (this.userinfo.year) {
					ydData = ydRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				if (type) {
					yd = ydData.map(d => {
						d.action = "none"
						imageInfos = imageInfos.concat(this.getYdzpArr(d))
						return d
					})
				} else { //更新
					yd = this.userdata.yd
					ydData.map(d => {
						let olddata = yd.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"
							imageInfos = imageInfos.concat(this.getYdzpArr())
							yd.push(d)
						}
					})
				}
				return {
					data: yd,
					images: imageInfos
				}
			},
			async downloadyf(surveyUser, type) {
				let yfRes = await getYfList({
					surveyUser
				})
				let yfData = yfRes.data
				let imageInfos = []
				let yf = null
				if (this.userinfo.year) {
					yfData = yfRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				if (type) {
					yf = yfData.map(d => {
						d.action = 'none'
						imageInfos = imageInfos.concat(this.getYfzpArr(d))
						return d
					})
				} else {
					yf = this.userdata.yf
					yfData.map(d => {
						let olddata = yf.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"
							imageInfos = imageInfos.concat(this.getYfzpArr())
							yf.push(d)
						}
					})
				}
				return {
					data: yf,
					images: imageInfos
				}
			},
			async downloadbiomass(surveyUser, type) {
				let imageInfos = []
				let biomass = null
				let biomassRes = await getBiomass({
					surveyUser
				})
				let biomassData = biomassRes.data
				if (this.userinfo.year) {
					biomassData = biomassRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				if (type) {
					biomass = biomassData.map(d => {
						d.action = 'none'
						let zps = this.getyfzpOtther(d, "biomass")

						if (zps && zps.length > 0) {
							imageInfos = imageInfos.concat(zps)
						}

						return d
					})
				} else {
					biomass = this.userdata.biomass
					biomassData.map(d => {
						let olddata = biomass.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"
							let zps = this.getyfzpOtther(d, "biomass")
							if (zps && zps.length > 0) {
								imageInfos = imageInfos.concat(zps)
							}
							biomass.push(d)
						}
					})
				}
				return {
					data: biomass,
					images: imageInfos
				}
			},
			async downloadenv(surveyUser, type) {
				let envRes = await getEnvironment({
					surveyUser
				})
				let env = null
				let envData = envRes.data
				if (this.userinfo.year) {
					envData = envRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				let imageInfos = []
				if (type) {
					env = envData.map(d => {
						d.action = 'none'
						let zps = this.getyfzpOtther(d, "env")
						if (zps && zps.length > 0) {
							imageInfos = imageInfos.concat(zps)
						}
						return d
					})
				} else {
					env = this.userdata.env
					envData.map(d => {
						let olddata = env.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"
							let zps = this.getyfzpOtther(d, "env")
							if (zps && zps.length > 0) {
								imageInfos = imageInfos.concat(zps)
							}
							env.push(d)
						}
					})
				}
				return {
					data: env,
					images: imageInfos
				}
			},
			async downloadGrassType(surveyUser, type) {

				let grassTypeRes = await getGrasslandType({
					surveyUser
				})

				let grassTypeData = grassTypeRes.data
				let grassType = null
				let imageInfos = []
				if (this.userinfo.year) {
					grassTypeData = grassTypeRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				if (type) {
					grassType = grassTypeData.map(d => {
						d.action = 'none'

						let zps = this.getyfzpOtther(d, "grassType")
						if (zps && zps.length > 0) {
							imageInfos = imageInfos.concat(zps)
						}
						return d
					})
				} else {
					grassType = this.userdata.grassType
					grassTypeData.map(d => {
						let olddata = grassType.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"
							let zps = this.getyfzpOtther(d, "grassType")
							if (zps && zps.length > 0) {
								imageInfos = imageInfos.concat(zps)
							}
							grassType.push(d)
						}
					})
				}
				return {
					data: grassType,
					images: imageInfos
				}

			},
			async downloadSubBiomass(surveyUser, type) {
				//底下生物量
				let subBiomassRes = await getSubsurfaceBiomass({
					surveyUser
				})
				let subBiomass = null
				let imageInfos = []
				let subBiomassData = subBiomassRes.data
				if (this.userinfo.data) {
					subBiomassData = subBiomassRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				if (type) {
					subBiomass = subBiomassData.map(d => {
						d.action = 'none'

						let zps = this.getyfzpOtther(d, "subBiomass")
						if (zps && zps.length > 0) {
							imageInfos = imageInfos.concat(zps)
						}
						return d
					})
				} else {
					subBiomass = this.userdata.subBiomass
					subBiomassData.map(d => {
						let olddata = subBiomass.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"
							let zps = this.getyfzpOtther(d, "subBiomass")
							if (zps && zps.length > 0) {
								imageInfos = imageInfos.concat(zps)
							}
							subBiomass.push(d)
						}
					})
				}
				return {
					data: subBiomass,
					images: imageInfos
				}

			},
			async downloadBrush(surveyUser, type) {
				//底下生物量
				let brushRes = await getBrushSurveyList({
					surveyUser
				})
				let brushData = brushRes.data
				let brush = null
				if (this.userinfo.year) {
					brushData = brushRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				if (type) {
					brush = brushData.map(d => {
						d.action = 'none'
						return d
					})
				} else {
					brush = this.userdata.brush
					brushData.map(d => {
						let olddata = brush.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"

							brush.push(d)
						}
					})
				}
				return {
					data: brush,
				}
			},
			async downloadFrequencys(surveyUser, type) {
				//底下生物量
				let frequencyRes = await getFrequencys({
					surveyUser
				})
				let frequencyData = frequencyRes.data
				let frequency = null
				if(this.userinfo.data){
					frequencyData = frequencyRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				if (type) {
					frequency = frequencyData.map(d => {
						d.action = 'none'
						return d
					})
				} else {
					frequency = this.userdata.frequency
					frequencyData.map(d => {
						let olddata = frequency.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"

							frequency.push(d)
						}
					})
				}
				return {
					data: frequency,

				}

			},
			async downloadRich(surveyUser, type) {
				//地下生物量
				let brushRes = await getRichness({
					surveyUser
				})
				let brushData = brushRes.data
				if(this.userinfo.year){
					brushData = brushRes.data.filter(t => t.ydNumber.substr(0, 4) == this.userinfo.year)
				}
				let brush = null

				if (type) {
					brush = brushData.map(d => {
						d.action = 'none'
						return d
					})
				} else {
					brush = this.userdata.rich
					brushData.map(d => {
						let olddata = brush.find(t => t.id == d.id)
						if (olddata) {
							if (olddata.action == 'none') {
								olddata = d
							}
						} else {
							d.action = "none"

							brush.push(d)
						}
					})
				}
				return {
					data: brush,

				}

			},

			getYdzpArr(d) {
				if (!d) return []
				let imageInfos = []
				let fqzp = []
				if (d.fqqzp) {
					fqzp = JSON.parse(d.fqqzp)
					fqzp.map(t => {
						t.type = "yd"
						t.ydNumber = d.ydNumber
						t.yfNumber = null
						return t
					})
				}
				let sqzp = []
				if (d.sqzp) {
					sqzp = JSON.parse(d.sqzp)
					sqzp.map(t => {
						t.type = "yd"
						t.ydNumber = d.ydNumber
						t.yfNumber = null
						return t
					})
				}
				let khzp = []
				if (d.khqzp) {
					khzp = JSON.parse(d.khqzp)
					khzp.map(t => {
						t.type = "yd"
						t.ydNumber = d.ydNumber
						t.yfNumber = null
						return t
					})
				}
				return imageInfos.concat(fqzp, sqzp, khzp)
			},
			getYfzpArr(d) {
				if (!d) {
					return []
				}
				let fqzp = [],
					sqzp = [],
					khzp = [],
					yfzp = [],
					imageInfos = []
				if (d.fqqyfzp) {
					fqzp = JSON.parse(d.fqqyfzp)

					fqzp.map(t => {
						t.type = "yf"
						t.ydNumber = d.ydNumber
						t.yfNumber = d.yfNumber
						return t
					})
				}
				if (d.sqyfzp) {
					sqzp = JSON.parse(d.sqyfzp)
					sqzp.map(t => {
						t.type = "yf"
						t.ydNumber = d.ydNumber
						t.yfNumber = d.yfNumber
						return t
					})
				}
				if (d.khyfzp) {
					khzp = JSON.parse(d.khyfzp)
					khzp.map(t => {
						t.type = "yf"
						t.ydNumber = d.ydNumber
						t.yfNumber = d.yfNumber
						return t
					})
				}
				if (d.yfzp) {
					yfzp = JSON.parse(d.yfzp)
					yfzp.map(t => {
						t.type = "yf"
						t.ydNumber = d.ydNumber
						t.yfNumber = d.yfNumber
						return t
					})
				}
				return imageInfos.concat(fqzp, sqzp, khzp, yfzp)
			},
			getyfzpOtther(d, type) {
				if (!d) {
					return []
				}
				let imageInfos = []
				if (d.yfzp) {
					let zp = JSON.parse(d.yfzp)

					zp.map(t => {
						t.type = type
						t.ydNumber = d.ydNumber
						t.yfNumber = d.yfNumber
						return t
					})
					console.log(zp)
					return imageInfos.concat(zp)

				}
			},

			uploadownFie() {
				//上传数据
				if (this.userdata) {

				}
				//清空，
				//下载数据
				let yd = this.userdata.yd
				let yf = this.userdata.yf
				let grassType = this.userdata.grassType
				let subBiomass = this.userdata.subBiomass
				let env = this.userdata.env
				let biomass = this.userdata.biomass


				let imageInfos = []
				yd.forEach(d => {
					let fqzp = []
					if (d.fqqzp) {
						fqzp = JSON.parse(d.fqqzp)
						fqzp.map(t => {
							t.type = "yd"
							t.ydNumber = d.ydNumber
							t.yfNumber = null
							return t
						})
					}
					let sqzp = []
					if (d.sqzp) {
						sqzp = JSON.parse(d.sqzp)
						sqzp.map(t => {
							t.type = "yd"
							t.ydNumber = d.ydNumber
							t.yfNumber = null
							return t
						})
					}
					let khzp = []
					if (d.khqzp) {
						khzp = JSON.parse(d.khqzp)
						khzp.map(t => {
							t.type = "yd"
							t.ydNumber = d.ydNumber
							t.yfNumber = null
							return t
						})
					}
					imageInfos = imageInfos.concat(fqzp, sqzp, khzp)
				})
				yf.forEach(d => {
					console.log(d)
					let fqzp = [],
						sqzp = [],
						khzp = [],
						yfzp = []
					if (d.fqqyfzp) {
						fqzp = JSON.parse(d.fqqyfzp)
						console.log("111")
						fqzp.map(t => {
							t.type = "yf"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
					}
					if (d.sqyfzp) {
						sqzp = JSON.parse(d.sqyfzp)
						sqzp.map(t => {
							t.type = "yf"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
					}
					if (d.khyfzp) {
						khzp = JSON.parse(d.khqzp)
						khzp.map(t => {
							t.type = "yf"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
					}
					if (d.yfzp) {
						yfzp = JSON.parse(d.yfzp)
						yfzp.map(t => {
							t.type = "yf"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
					}
					imageInfos = imageInfos.concat(fqzp, sqzp, khzp, yfzp)
				})
				grassType.forEach(d => {
					if (d.yfzp) {
						let zp = JSON.parse(d.yfzp)
						zp.map(t => {
							t.type = "grassType"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
						imageInfos = imageInfos.concat(fqzp, sqzp, khzp)
					}
				})
				subBiomass.forEach(d => {
					if (d.yfzp) {
						let zp = JSON.parse(d.yfzp)
						zp.map(d => {
							t.type = "subBiomass"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
						imageInfos = imageInfos.concat(fqzp, sqzp, khzp)
					}
				})
				env.forEach(d => {
					if (d.yfzp) {
						let zp = JSON.parse(d.yfzp)
						zp.map(d => {
							t.type = "env"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
						imageInfos = imageInfos.concat(fqzp, sqzp, khzp)
					}
				})
				biomass.forEach(d => {
					if (d.yfzp) {
						let zp = JSON.parse(d.yfzp)
						zp.map(d => {
							t.type = "biomass"
							t.ydNumber = d.ydNumber
							t.yfNumber = d.yfNumber
							return t
						})
						imageInfos = imageInfos.concat(fqzp, sqzp, khzp)
					}
				})

				this.progress = 0
				this.toggleOpen()
				this.downLoadfiles(imageInfos).then(res => {
					this.userdata["files"] = res
					setInfo("userdata", this.userdata).then(sucess => {
						this.toggleClose()
						uni.showToast({
							title: '下载完成',
							icon: 'checkmarkempty'
						})
					})
				})
			},
			async downLoadfiles(data) {
				let dataInfo = []
				let rate = 100 / data.length
				let index = 0
				for (const d of data) {
					let dres = await downLoad(d.url)
					index++
					this.progress = Math.round(index * rate)
					if (dres) {
						let file = {
							name: d.name,
							uid: d.uid,
							url: d.url,
							localurl: dres.savedFilePath,
							action: 'none',
							type: d.type,
							ydNumber: d.ydNumber,
							yfNumber: d.yfNumber,
							id: (new Date()).getTime()
						}
						dataInfo.push(file)
					}
				}
				return dataInfo

			},
			checkStatus(yd, yfs) {
				let check = true;
				if (yd.surveyType) {
					let types = yd.surveyType.split(',')
					types.forEach(d => {
						let yf = yfs.filter(t => t.ydNumber == yd.ydNumber && t.surveyType == d)
						if (yf <= 0) {
							check = false
						}
					})
				}
				return check

			},
			async upload() {
				let that = this
				if (!this.userdata) {
					return
				}
				let yddata = this.userdata.yd
				let yfdata = this.userdata.yf
				let grassType = this.userdata.grassType
				let subBiomass = this.userdata.subBiomass
				let env = this.userdata.env
				let biomass = this.userdata.biomass
				let brush = this.userdata.brush
				let frequency = this.userdata.frequency
				let rich = this.userdata.rich
				let files = this.userdata.files || []
				let upload = await this.uploadImages(files)
				//样地
				this.popueTitle = "开始上传样地采集信息"
				this.progress = 0
				// let ydindex = 0
				// this.toggleOpen()
				//yf
				for (const yd of yddata) {
					let check = that.checkStatus(yd, yfdata)
					console.log(check)
					if (check) {
						yd.status = 1
						yd.action = 'edit'
					}
					if (yd.action != 'none') {
						//处理返青照片
						if (yd.fqqzp) {
							let fzzp = JSON.parse(yd.fqqzp)
							let upfzzp = []
							for (const ydfqzp of fzzp) {
								if (ydfqzp.action != 'none') {
									let ydzpinfo = upload.find(t => t.id == ydfqzp.id)
									if (ydzpinfo) {
										let czp = {
											name: ydfqzp.name,
											url: ydzpinfo.url,
											uid: ydzpinfo.uid,

										}

										ydzpinfo.action = 'none'
										upfzzp.push(czp)
									}
								} else {
									let czp = {
										name: ydfqzp.name,
										url: ydfqzp.url,
										uid: ydfqzp.uid
									}
									upfzzp.push(czp)
								}
							}
							yd.fqqzp = JSON.stringify(upfzzp)
						}

						//盛期
						if (yd.sqzp) {
							let sqzp = JSON.parse(yd.sqzp)
							let updqzp = []
							for (const ydsqzp of sqzp) {
								if (ydsqzp.action != 'none') {
									let ydsqzpinfo = upload.find(t => t.id == ydsqzp.id)
									if (ydsqzpinfo) {
										let czp = {
											name: ydsqzp.name,
											url: ydsqzpinfo.url,
											uid: ydsqzpinfo.uid,

										}

										ydsqzpinfo.action = 'none'

										updqzp.push(czp)
									}
								} else {
									let czp = {
										name: ydsqzp.name,
										url: ydsqzp.url,
										uid: ydsqzp.uid
									}
									updqzp.push(czp)
								}
							}
							yd.sqzp = JSON.stringify(updqzp)
						}

						//枯黄
						if (yd.khqzp) {
							let khzp = JSON.parse(yd.khqzp)
							let ydkhzps = []
							for (const ydkhzp of khzp) {
								if (ydkhzp.action != 'none') {
									let ydkhzpinfo = upload.find(t => t.id == ydkhzp.id)
									if (ydkhzpinfo) {
										let czp = {
											name: ydkhzp.name,
											url: ydkhzpinfo.url,
											uid: ydkhzpinfo.uid,

										}
										ydkhzpinfo.action = 'none'
										ydkhzps.push(czp)
									}
								} else {
									let czp = {
										name: ydkhzp.name,
										url: ydkhzp.url,
										uid: ydkhzp.uid
									}
									ydkhzps.push(czp)

								}
							}
							yd.khqzp = JSON.stringify(ydkhzps)
						}
						//提交
						await addOrUpdateYd(yd)
					}
					yd.action = 'none'
					// ydindex++;
					// that.progress = Math.round(ydindex * 100 / yd.length)
				}
				// this.toggleClose()

				this.popueTitle = "开始上传样方采集信息"
				this.progress = 10
				// let yfindex = 0
				// this.toggleOpen()

				for (const yf of yfdata) {
					if (yf.action == 'delete') {
						await delYf(yf.id)
					} else if (yf.action != 'none') {
						//返青
						if (yf.fqqyfzp) {
							let yffqzp = JSON.parse(yf.fqqyfzp)
							let yffqzps = []
							for (const yffqzp of yffqzp) {
								if (yffqzp.action != 'none') {
									let info = upload.find(t => t.id == ydkhzp.id)
									let czp = {
										name: yffqzp.name,
										url: info.url,
										uid: info.uid,

									}
									info.action = 'none'
									yffqzps.push(czp)
								} else {
									let czp = {
										name: yffqzp.name,
										url: yffqzp.url,
										uid: yffqzp.uid
									}
									yffqzps.push(czp)
								}
							}
							yf.fqqyfzp = JSON.stringify(yffqzps)
						}
						if (yf.sqyfzp) {
							//盛期
							let yfsqzp = JSON.parse(yf.sqyfzp)
							let yfsqzps = []
							for (const yfsqzp of yfsqzp) {
								if (yfsqzp.action != 'none') {
									let info = upload.find(t => t.id == yfsqzp.id)
									let czp = {
										name: yfsqzp.name,
										url: info.url,
										uid: info.uid,

									}
									info.action = 'none'
									yfsqzps.push(czp)
								} else {
									let czp = {
										name: yfsqzp.name,
										url: yfsqzp.url,
										uid: yfsqzp.uid
									}
									yfsqzps.push(czp)
								}
							}
							yf.sqyfzp = JSON.stringify(yfsqzps)
						}
						if (yf.khqzp) {
							//枯黄
							let yfkuzp = JSON.parse(yf.khqzp)
							let yfkhzps = []
							for (const yfkhzp of yfkuzp) {
								if (yfkhzp.action != 'none') {
									let info = upload.find(t => t.id == yfkhzp.id)
									let czp = {
										name: yfkhzp.name,
										url: info.url,
										uid: info.uid,

									}
									info.action = 'none'
									yfkhzps.push(czp)
								} else {
									let czp = {
										name: yfkhzp.name,
										url: yfkhzp.url,
										uid: yfkhzp.uid
									}
									yfkhzps.push(czp)
								}
							}
							yf.khqzp = JSON.stringify(yfkhzps)
						}

						//yfzp
						if (yf.yfzp) {
							let yfzp = JSON.parse(yf.yfzp)
							let yfzps = []
							for (const zp of yfzp) {
								if (zp.action != 'none') {
									let info = upload.find(t => t.id == zp.id)
									let czp = {
										name: zp.name,
										url: info.url,
										uid: info.uid,

									}
									info.action = 'none'
									yfzps.push(czp)
								} else {
									let czp = {
										name: zp.name,
										url: zp.url,
										uid: zp.uid
									}
									yfzps.push(czp)
								}
							}
							yf.yfzp = JSON.stringify(yfzps)
						}
						addOrUpdateYf(yf)
					}
					// yfindex++
					// this.progress = Math.round(yfindex * 100 / yf.length)
				}
				// this.toggleClose()


				//草地类型
				this.popueTitle = "开始上传草地类型采集信息"
				this.progress = 20
				// let gtindex = 0
				// this.toggleOpen()
				for (const gt of grassType) {
					if (gt.action != 'none') {
						if (gt.yfzp) {


							let gtzp = JSON.parse(gt.yfzp)
							let gtzps = []
							for (const zp of gtzp) {
								if (zp.action != 'none') {
									let info = upload.find(t => t.id == zp.id)
									let czp = {
										name: zp.name,
										url: info.url,
										uid: info.uid,

									}
									info.action = 'none'
									gtzps.push(czp)

								} else {
									let czp = {
										name: zp.name,
										url: zp.url,
										uid: zp.uid
									}
									gtzps.push(czp)
								}
							}
							gt.yfzp = JSON.stringify(gtzps)
						}
						await addOrUpdateGrasslandType(gt)
					}
					// gtindex++
					// this.progress = Math.round(gtindex * 100 / grassType.length)
				}
				// this.toggleClose()

				//地下生物量
				this.popueTitle = "开始上传地下生物量采集信息"
				this.progress = 30
				// let sbindex = 0
				// this.toggleOpen()
				for (const sb of subBiomass) {
					if (sb.action != 'none') {
						if (sb.yfzp) {


							let sbzp = JSON.parse(sb.yfzp)
							let sbzps = []
							for (const zp of sbzp) {
								if (zp.action != 'none') {
									let info = upload.find(t => t.id == zp.id)
									let czp = {
										name: zp.name,
										url: info.url,
										uid: info.uid
									}
									info.action = 'none'
									sbzps.push(czp)
								} else {
									let czp = {
										name: zp.name,
										url: zp.url,
										uid: zp.uid
									}
									sbzps.push(czp)
								}
							}
							sb.yfzp = JSON.stringify(sbzps)
						}
						await addOrUpdateSubsurfaceBiomass(sb)
					}
					// sbindex++
					// this.progress = Math.round(sbindex * 100 / subBiomass.length)
				}
				// this.toggleClose()

				//温湿度
				this.popueTitle = "开始上传温湿度采集信息"
				this.progress = 40
				// let envindex = 0
				// this.toggleOpen()
				for (const tt of env) {
					if (tt.action != 'none') {
						if (tt.yfzp) {
							let ttzp = JSON.parse(tt.yfzp)
							let ttzps = []
							for (const zp of ttzp) {
								if (zp.action != 'none') {
									let info = upload.find(t => t.id == zp.id)
									let czp = {
										name: zp.name,
										url: info.url,
										uid: info.uid
									}
									info.action = 'none'
									ttzps.push(czp)
								} else {
									let czp = {
										name: zp.name,
										url: zp.url,
										uid: zp.uid
									}
									ttzps.push(czp)
								}
							}
							tt.yfzp = JSON.stringify(ttzps)
						}
						await addOrUpdateEnvironment(tt)
					}
					// envindex++
					// this.progress = Math.round(envindex * 100 / env.length)
				}
				// this.toggleClose()

				//生物量

				this.popueTitle = "开始上传温湿度采集信息"
				this.progress = 50
				// let biindex = 0
				// this.toggleOpen()
				for (const bi of biomass) {
					if (bi.action != 'none') {
						if (bi.yfzp) {


							let bizp = JSON.parse(bi.yfzp)
							let bizps = []
							for (const zp of bizp) {
								if (zp.action != 'none') {
									let info = upload.find(t => t.id == zp.id)
									let czp = {
										name: zp.name,
										url: info.url,
										uid: info.uid
									}
									info.action = 'none'
									bizps.push(czp)
								} else {
									let czp = {
										name: zp.name,
										url: zp.url,
										uid: zp.uid
									}
									bizps.push(czp)
								}
							}
							bi.yfzp = JSON.stringify(bizps)
						}
						await addOrUpdateBiomass(bi)
					}
					// biindex++
					// this.progress = Math.round(biomass * 100 / env.length)
				}

				// this.toggleClose()


				//开始灌丛
				this.popueTitle = "开始上传灌丛采集信息"
				this.progress = 60
				// let brindex = 0
				// this.toggleOpen()
				for (const br of brush) {
					if (br.action != 'none' && br.action != 'delete') {
						await addOrUpdateBrushSurvey(br)
					}
					// brindex++
					// this.progress = Math.round(brindex * 100 / brush.length)
				}

				// this.toggleClose()

				// 频度
				this.popueTitle = "开始上传频度信息"
				this.progress = 70
				// let frindex = 0
				// this.toggleOpen()
				// for (const fr of frequency) {
				// 	if (fr.action != 'none' && fr.action != 'delete') {
				await addFrequencys(frequency)
				// }
				// frindex++
				// this.progress = Math.round(frindex * 100 / frequency.length)
				// }

				// this.toggleClose()


				//  丰富度			   
				this.popueTitle = "开始上传丰富度信息"
				this.progress = 90
				// let riindex = 0
				// this.toggleOpen()
				for (const fr of rich) {
					if (fr.action != 'none' && fr.action != 'delete') {
						await addOrUpdateRichness(fr)
					}
					// riindex++
					// this.progress = Math.round(riindex * 100 / rich.length)
				}

				this.toggleClose()


			},

			async uploadImages(data) {
				// let reslist = await filesList()
				// console.log(reslist)
				// let upres = await uploadImage(reslist[0].filePath)

				// {"data":{"name":"17027020321200","url":"/muli-file/image/thumbFile/1702705126903.jpg"}
				this.popueTitle = "正在上传资源文件"
				this.progress = 0

				this.toggleOpen()
				let rate = data.length / 100
				let index = 0
				let newdata = []
				for (const d of data) {
					if (d.action == 'edit') {
						let uploadImg = await uploadImage(d.localurl)
						let img = JSON.parse(uploadImg).data
						d.action == 'none'
						d.uid = img.name,
							d.url = img.url

					}
					newdata.push(d)
					index++
					this.progress = index * rate
				}
				this.toggleClose()
				return newdata

			},
			toggleOpen() {
				console.log("aaaa")
				this.$refs.popup.open('center')
			},
			toggleClose() {
				this.$refs.popup.close()
			},
			getsurveryCount() {
				let yd = this.userdata.yd.length + this.userdata.yf.length
				return yd
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		width: 100%;
		box-sizing: border-box;
		padding: 10rpx 30rpx;
		padding-top: 300rpx;
		padding-bottom: 60rpx;
		font-size: 28rpx;

		&::after {
			content: '';
			display: block;
			background-color: #2979ff;
			width: 100%;
			height: 30vh;
			position: fixed;
			top: 0;
			left: 0;
			border-bottom-right-radius: 100rpx;
			border-bottom-left-radius: 100rpx;
			z-index: 1;
		}

		.content {
			width: 100%;
			box-sizing: border-box;
			padding: 10rpx 20rpx;
			background-color: #fff;
			border-radius: 12rpx;
			position: relative;
			z-index: 10;
			margin-bottom: 20rpx;
		}

		.user-info-content {
			box-sizing: border-box;
			padding-top: 50rpx;

			.user-info-img {
				width: 150rpx;
				height: 150rpx;
				border-radius: 50%;
				overflow: hidden;
				background-color: #fff;
				top: -120rpx;
				left: 50%;
				transform: translateX(-50%);
				position: absolute;

				image {
					width: 100%;
					height: auto;
				}

			}

			.user-info {
				text-align: center;
				font-size: 28rpx;
				margin-bottom: 20rpx;

				.name {
					position: relative;

					.job {
						background-color: #facd91;
						color: #fff;
						position: absolute;
						right: 50rpx;
						display: inline-block;
						box-sizing: border-box;
						padding: 0 10rpx;
						height: 1.6em;
						line-height: 1.6em;
						border-radius: .8em;
					}
				}
			}

			.record {
				width: 100%;
				font-size: 28rpx;
				color: #666;
				position: relative;
				box-sizing: border-box;
				padding: 10rpx 20rpx;
				border-bottom: 1rpx #f1f1f1 solid;
				margin-bottom: 20rpx;

				&::after {
					display: inline-block;
					font-size: 36rpx;
					content: ">";
					position: absolute;
					right: 20rpx;
					color: #999;
				}
			}

			.plot-wrap {
				display: flex;
				justify-content: space-around;
				align-items: center;
				box-sizing: border-box;
				padding: 20rpx 10rpx;
				text-align: center;

				text {
					font-weight: bold;
					color: #666;
					font-size: 46rpx;
					padding: 0 10rpx;
				}
			}
		}

		.data-wrap {
			display: flex;
			justify-content: space-around;
			align-items: center;
			box-sizing: border-box;
			padding: 20rpx 10rpx;
			text-align: center;

			text {
				font-weight: bold;
				color: #666;
				font-size: 46rpx;
				padding: 0 10rpx;
			}
		}

		.menu-item {
			width: 100%;
			font-size: 28rpx;
			color: #666;
			position: relative;
			box-sizing: border-box;
			padding: 10rpx 20rpx;
			border-bottom: 1rpx #f1f1f1 solid;
			margin-bottom: 20rpx;

			&::after {
				display: inline-block;
				font-size: 36rpx;
				content: ">";
				position: absolute;
				right: 20rpx;
				color: #999;
			}
		}
	}

	.popup {
		width: 100%;
		height: 100rpx;
	}

	.progress-box {
		width: 100vw;
		// width: 100%;
		display: flex;
		height: 50rpx;

	}
</style>